package com.td.module.agro.controller.admin.input;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.agro.controller.admin.input.vo.*;
import com.td.module.agro.convert.input.InputConvert;
import com.td.module.agro.dal.dataobject.input.InputDO;
import com.td.module.agro.service.input.InputService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 投入品")
@RestController
@RequestMapping("/agro/input")
@Validated
public class InputController {

    @Resource
    private InputService inputService;

    @PostMapping("/create")
    @ApiOperation("创建投入品")
    @PreAuthorize("@ss.hasPermission('agro:input:create')")
    public CommonResult<Long> createInput(@Valid @RequestBody InputCreateReqVO createReqVO) {
        return success(inputService.createInput(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新投入品")
    @PreAuthorize("@ss.hasPermission('agro:input:update')")
    public CommonResult<Boolean> updateInput(@Valid @RequestBody InputUpdateReqVO updateReqVO) {
        inputService.updateInput(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除投入品")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('agro:input:delete')")
    public CommonResult<Boolean> deleteInput(@RequestParam("id") Long id) {
        inputService.deleteInput(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得投入品")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('agro:input:query')")
    public CommonResult<InputRespVO> getInput(@RequestParam("id") Long id) {
        InputDO input = inputService.getInput(id);
        return success(InputConvert.INSTANCE.convert(input));
    }

    @GetMapping("/list")
    @ApiOperation("获得投入品列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('agro:input:query')")
    public CommonResult<List<InputRespVO>> getInputList(@RequestParam("ids") Collection<Long> ids) {
        List<InputDO> list = inputService.getInputList(ids);
        return success(InputConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得投入品分页")
    @PreAuthorize("@ss.hasPermission('agro:input:query')")
    public CommonResult<PageResult<InputRespVO>> getInputPage(@Valid InputPageReqVO pageVO) {
        PageResult<InputDO> pageResult = inputService.getInputPage(pageVO);
        return success(InputConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出投入品 Excel")
    @PreAuthorize("@ss.hasPermission('agro:input:export')")
    @OperateLog(type = EXPORT)
    public void exportInputExcel(@Valid InputExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InputDO> list = inputService.getInputList(exportReqVO);
        // 导出 Excel
        List<InputExcelVO> datas = InputConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "投入品.xls", "数据", InputExcelVO.class, datas);
    }

}
